package com.aikg.kgrag_java.utils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TimeParser {
    public static int Converter(String chineseNumber) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(chineseNumber);
        while (matcher.find()) {
            return Integer.parseInt(matcher.group());
        }
        String[] chineseNumbers = {
                "二零一九", "二零二零", "二零二一", "二零二二", "二零二三", "二零二四", "二零二五", "二零二六",
                "二十", "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十", "三十一",
                "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九",
                "零", "一", "二", "两", "三", "四", "五", "六", "七", "八", "九", "十", "腊"
        };

        int[] arabicNumbers = {
                2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026,
                20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
                11, 12, 13, 14, 15, 16, 17, 18, 19,
                0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12
        };
        int i = 0;
        for (; i < chineseNumbers.length; i++) {
            if (chineseNumbers[i].equals(chineseNumber)) {
                return arabicNumbers[i];
            }
        }
        return -1;
    }
    /**
     * 时间表达分情况讨论，比如：七号到八号
     *
     * @param expression BERT识别结果
     * @return 返回切分好的时间信息，ArrayList<String>
     */
    public static ArrayList<String> InitExpression(String expression){
        if (expression.equals("")){
            expression = "2024-1-1";
        }
        ArrayList<String> expressionList = new ArrayList<>();
        ArrayList<String> result = new ArrayList<>();
        if (expression.contains("到")){
            String[] parts =expression.split("到");
            if (parts[0].contains("月")&&!parts[1].contains("月")){
                parts[1] = parts[0].split("月")[0] + "月" + parts[1];
            }
            if (parts[0].contains("年")&&!parts[1].contains("年")){
                parts[1] = parts[0].split("年")[0] + "年" + parts[1];
            }
            expressionList.add(parts[0]);
            expressionList.add(parts[1]);
            for (LocalDateTime dateTime:DoubleTime(expressionList)) {
                String dateString = dateTime.toString();
                result.add(dateString.substring(0,10));
            }
//            System.out.println(DoubleTime(expressionList));
        }else {
            expressionList.add(expression);
            for (LocalDateTime dateTime:SingleTime(expressionList)) {
                String dateString = dateTime.toString();
                result.add(dateString.substring(0,10));
            }
//            System.out.println(SingleTime(expressionList));
        }
        return result;
    }

    /**
     * 计算：两个时间形成的时间列表
     *
     * @param expressionList 时间表达， ArrayList类型两个值
     * @return 两个时间形成的时间列表，可直接打印结果
     */
    private static ArrayList<LocalDateTime> DoubleTime(ArrayList<String> expressionList){
        String expression0 = expressionList.get(0);
        String expression1 = expressionList.get(1);
        LocalDateTime time0 = calculate(expression0);
        LocalDateTime time1 = calculate(expression1);
        ArrayList<LocalDateTime> result = new ArrayList<>();
        if (time1.isBefore(time0)){
            LocalDateTime i = time0;
            time0 = time1;
            time1 = i;
        }
        while (!time0.isAfter(time1)) {
            result.add(time0);
            // 当前日期加一天
            time0 = time0.plusDays(1);
        }
        return result;
    }

    /**
     * 计算：一个时间形成的时间列表（上面是两个）
     *
     * @param expressionList 时间表达， 虽然是ArrayList类型，但只有一个值
     * @return 一个时间形成的时间列表，可直接打印结果
     */
    private static ArrayList<LocalDateTime> SingleTime(ArrayList<String> expressionList){
        String expression = expressionList.get(0);
        LocalDateTime time = calculate(expression);
        ArrayList<LocalDateTime> result = new ArrayList<>();
        result.add(time);
        return result;
    }

    /**
     * 根据汉字，生成utc
     * 年，月，日
     *
     * @param expression 汉字
     * @return 2024-01-10
     */
    private static LocalDateTime calculate(String expression) {
        LocalDateTime now = LocalDateTime.now();

        //计算哪天
        if (expression.startsWith("今天") || expression.equals("现在")) {
            return now;
        } else if (expression.startsWith("昨天") ){
            return now.minusDays(1);
        } else if (expression.startsWith("前天") ){
            return now.minusDays(2);
        }
        if (expression.startsWith("去年")){
            expression = String.format("%04d", now.getYear() - 1) + expression.substring(1);
        }
        if (expression.startsWith("今年")){
            expression = String.format("%04d", now.getYear()) + expression.substring(1);
        }
//        System.out.println(expression);
        if (expression.endsWith("号")){
            expression = expression.substring(0,expression.length()-1);
        }
        String[] dateList = new String[0];
        if (expression.contains("月")){
            dateList = expression.split("年|月|日");//拆分年月日
        } else if (expression.contains("-")){
            dateList = expression.split("-");//拆分年月日
        } else if (expression.contains(".")){
            dateList = expression.split(Pattern.quote("."));//拆分年月日
        }
        for (int j = 0;j<dateList.length;j++) {
            if (dateList[j].startsWith("初")){
                dateList[j] = dateList[j].substring(1);
            }
        }
        return LocalDateTime.of(Converter(dateList[0]), Converter(dateList[1]), Converter(dateList[2]), 0, 0);
    }

//    public static void main(String[] args) {
//        String sentence = "2025年一月初一";
//        String sentence1 = "二零二四年十二月二十八日到二零二五年一月一日";
//        String sentence2 = "2024-1-2";
//        String sentence3 = "2024.1.1";
//        String sentence4 = "";
//        System.out.println(InitExpression(sentence));
//        System.out.println(InitExpression(sentence1));
//        System.out.println(InitExpression(sentence2));
//        System.out.println(InitExpression(sentence3));
//        System.out.println(InitExpression(sentence4));
//    }
}
